package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ImageViewPagerActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ItemHolder> {

    private Context context;
    private String[] images;
    private int smallImgWidth;

    public ImagesAdapter(Context context,int smallImgWidth) {
        this.context = context;
        this.smallImgWidth = smallImgWidth;
    }

    public void addData(String[] images) {
        this.images = images;
        notifyDataSetChanged();
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_images, parent, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_iamge);
        imageView.getLayoutParams().width = smallImgWidth;
        imageView.getLayoutParams().height = smallImgWidth;
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, final int position) {

        ImageView imageView = (ImageView) holder.itemView.findViewById(R.id.iv_iamge);

        Glide.with(context).load(images[position]).into(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageViewPagerActivity.start(context, images, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return images == null ? 0 : images.length;
    }

    class ItemHolder extends RecyclerView.ViewHolder {

//        @BindView(R.id.iv_iamge)
//        ImageView iv_iamge;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}